<?php

class Exts_Models_Users extends Exts_Db_Table_Abstract {

	protected $_name = 'us_users';
	protected $_primary = 'user_id';
	protected $_rowClass = 'Exts_Models_UsersRow';

	public function save($bind) {

		$primary = $this->_primary;
		if (is_array($this->_primary)) {
			$primary = $this->_primary[1];
		}

		if (isset($bind[$primary])) {
			$row = $this->find($bind[$primary])->current();
			if (!count($row)) {
				$row = $this->createRow();
			}
		} else {
			$row = $this->createRow();
		}
		if (isset($bind['password'])) {
			$bind['password'] = $this->_encrypt($bind['password']);
		}

		$row->setFromArray($bind);
		return $row->save();
	}

	public function getFullUser($id) {
		$row = $this->getAdapter()->fetchRow(
					$this->getAdapter()->select()
					->from(array('us' => 'us_users'), array('*'))
					->joinLeft(array('ur' => 'acl_users_roles'), 'ur.user_id=us.user_id', array())
					->joinLeft(array('ro' => 'acl_roles'), 'ro.id=ur.role_id', array('*'))
					->where('us.user_id=?', $id)
					->limit(1)
		);
		return $row;
	}

	public function getRole($id) {
		$row = $this->getAdapter()->fetchRow(
					$this->getAdapter()->select()
					->from(array('ur' => 'acl_users_roles'), array())
					->join(array('ro' => 'acl_roles'), 'ro.id=ur.role_id', array('name'))
					->where('ur.user_id=?', $id)
					->limit(1)
		);
		if (count($row)) {
			return $row['name'];
		}
		return "guest";
	}

	public function exists($username) {

		$row = $this->fetchRow(
				$this->select()->where('username=?', $username)
		);

		if (count($row)) {
			return $row;
		}
		return false;
	}

	public function getUserByUsername($username) {
		$row = $this->fetchRow(
				$this->select()->where('username=?', $username)
		);
		if (count($row)) {
			return $row;
		}
		return NULL;
	}

	public function isValidUser($user, $password) {
		if (!crypt($password, $user->password) == $user->password) {
			return false;
		}
		return true;
	}

	public function getIdByName($name) {
		$row = $this->fetchRow(
				$this->select()->where('username=?', $name)
		);
		if (count($row)) {
			return $row->user_id;
		}
		return NULL;
	}

	private function _encrypt($password) {
		return crypt($password, "$1$" . substr(uniqid(), 0, 7) . "$");
	}

	public function login($username, $password) {
		try {

			$user = $this->exists($username);

			if (!( crypt($password, $user->password) == $user->password )) {
				return false;
			}
			$dbAdapter = $this->getAdapter();
			$auth = Zend_Auth::getInstance();
			$adapter = new Zend_Auth_Adapter_DbTable(
					$dbAdapter, 'us_users', 'username', 'password'
			);
			$adapter->setIdentity($username);
			$adapter->setCredential($user->password);
			$result = $auth->authenticate($adapter);
			return $result->isValid();
		} catch (Excetion $e) {
			throw new Exts_Exception($e);
			exit;
			var_dump($user);
			exit;
			//return false;
		}
	}

	public function getAllActiveUser() {
		$rowset = $this->fetchAll();

		if (count($rowset)) {
			$u = array();
			foreach ($rowset as $row) {
				$u[$row['username']] = $row['username'];
			}
			return $u;
		}
		return array();
	}

	public function getAllUsersByRole($role) {
		$stmt = $this->getAdapter()->select()
				->from(array('rol' => 'acl_roles'), array())
				->join(array('ur' => 'acl_users_roles'), 'ur.role_id=rol.id', array())
				->join(array('us' => 'us_users'), 'us.user_id=ur.user_id')
				->where('rol.name=?', $role);


		return $this->getAdapter()->fetchAll(
			$stmt
		);
	}

}

